/*
 ============================================================================
 Name        : lua_c_project.c
 Author      : liu.sh
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include "read_xml_cnf.h"

extern int __init__lua(int argc, char **argv);


#if 0
int main(void) {
	puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */

	char buff[256];
	int error;
	lua_State *L = luaL_newstate(); /* opens Lua */
	luaL_openlibs(L);
	while (fgets(buff, sizeof(buff), stdin) != NULL) {
		error = luaL_loadbuffer(L, buff, strlen(buff),
		"line") || lua_pcall(L, 0, 0, 0);
		if (error) {
			fprintf(stderr, "%s", lua_tostring(L, -1));
			lua_pop(L, 1);/* pop error message from the stack */
		}
	}
	lua_close(L);

	return EXIT_SUCCESS;
}
#endif

#if 0

static void stackDump (lua_State *L) {
	int i;
	int top = lua_gettop(L);
	for (i = 1; i <= top; i++) { /* repeat for each level */
	int t = lua_type(L, i);
	switch (t) {
	case LUA_TSTRING: /* strings */
	printf("`%s'", lua_tostring(L, i));
	break;
	case LUA_TBOOLEAN: /* booleans */
	printf(lua_toboolean(L, i) ? "true" : "false");
	break;
	case LUA_TNUMBER: /* numbers */
	printf("%g", lua_tonumber(L, i));
	break;
	default: /* other values */
	printf("%s", lua_typename(L, t));
	break;
	}
	printf(" "); /* put a separator */
	}
	printf("\n"); /* end the listing */
}

int main (void) {
	lua_State *L = luaL_newstate();
	luaL_openlibs(L);
	lua_pushboolean(L, 1);
	lua_pushnumber(L, 10);
	lua_pushnil(L);
	lua_pushstring(L, "hello");
	stackDump(L);
	lua_settop(L, -3);
	stackDump(L);
	lua_close(L);
	return 0;
}

#endif


#if 0

#define MAX_COLOR 255
/* assume that table is on the stack top */
int getfield (lua_State *L, const char *key) {
	int result;
	lua_pushstring(L, key);
	printf("stact size is [%d]\n", lua_gettop(L));
	lua_gettable(L, -2); /* get background[key] */
	printf("stact size is [%d]\n", lua_gettop(L));
	if (!lua_isnumber(L, -1))
	printf("invalid component in background color");
	result = (int)lua_tonumber(L, -1);
	printf("stact size is [%d]\n", lua_gettop(L));
	lua_pop(L, 1); /* remove number */
	printf("stact size is [%d]\n", lua_gettop(L));
	return result;
}
int main(int argc, char **argv ) {
	char *pfile = NULL;
	int red,green,blue;
	lua_State *L = luaL_newstate(); /* opens Lua */
	luaL_openlibs(L);

	if (2 != argc) {
		printf("please input a file\n");
		exit(1);
	}

	pfile = (char *)argv[1];

	if(luaL_loadfile(L, pfile) || lua_pcall(L, 0, 0, 0))
	{
		printf("cannot run configuration file: %s",
				lua_tostring(L, -1));
	}
	printf("stact size is [%d]\n", lua_gettop(L));
	lua_getglobal(L, "background");
	printf("stact size is [%d]\n", lua_gettop(L));
	if (!lua_istable(L, -1))
		printf("`background' is not a valid color table");
	red = getfield(L,"r");
	green = getfield(L,"g");
	blue = getfield(L,"b");
	lua_close(L);

	printf("red=[%d],green=[%d],blue=[%d]\n", red,green,blue);

	my_create_xml();
	return EXIT_SUCCESS;
}

#else

/* call a function `f' defined in Lua */
char *get_res(lua_State *L, char *cmd)
{
	char *z;
	/* push functions and arguments */

	lua_getglobal(L, "get_res");

	lua_pushstring(L, cmd); /* push 1st argument */

	if (lua_pcall(L, 1, 1, 0) != 0)
		printf("error running function `get_res': %s\n", lua_tostring(L, -1));
	//printf("stact size is [%d]\n", lua_gettop(L));
	/* retrieve result */
	if (!lua_isstring(L, -1))
		printf("function `get_res' must return a string\n");
	z = lua_tostring(L, -1);

	lua_pop(L, 1);
	//printf("stact size is [%d]\n", lua_gettop(L));

	if(z)
		return (strdup(z));
	else
		printf("z is null\n");
		return NULL;
}

/* call a function `f' defined in Lua */
char *get_cmd(lua_State *L)
{
	char *z;
	int a;
	lua_getglobal(L, "get_cmd");

	if (lua_pcall(L, 0, 1, 0) != 0)
		printf("error running function `get_cmd': %s\n", lua_tostring(L, -1));
	/* retrieve result */
	if (!lua_isstring(L, -1))
		printf("function `get_cmd_list' must return a string\n");
	z = lua_tostring(L, -1);

	lua_pop(L, 1);
	if(z)
		return (strdup(z));
	else
		return NULL;
}


int main(int argc, char **argv ) {

	__init__lua(argc, argv);
	return 0;

	char *pfile = NULL;
	int red,green,blue;
	lua_State *L = luaL_newstate(); /* opens Lua */
	luaL_openlibs(L);

	if (2 != argc) {
		printf("please input a file\n");
		exit(1);
	}

	pfile = (char *)argv[1];

	if(luaL_loadfile(L, pfile) || lua_pcall(L, 0, 0, 0))
	{
		printf("cannot run configuration file: %s",
				lua_tostring(L, -1));
	}
	char *cmd;
	while(cmd = get_cmd(L)){
		printf("cmd=[%s]\n", cmd);
		printf("res=[%s]\n", get_res(L, cmd));
	}

	lua_close(L);
	return EXIT_SUCCESS;
}

/*
int main(int argc, char **argv ) {
	char pchar[32] = "23123123123123123123123";
	const char src[5] = "abcd";
	char *p = NULL;
	char *s = NULL;

	s = strdup(p);

	printf("pchar=[%s]\n", s);
	strncpy(pchar, src, strlen(src));

	printf("pchar=[%s]\n", pchar);

	return EXIT_SUCCESS;
}
*/
#endif
